﻿using System.IO;
using System.Windows.Forms;
using System.Text;
using System;
using System.Text.RegularExpressions;


namespace Replace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e) // Wczytywanie pliku
        {
            OpenFileDialog openFile1 = new OpenFileDialog(); // Otwieranie pliku
          
            openFile1.DefaultExt = "*.txt";
            openFile1.Filter = "txt files (*.txt)|*.txt|srt files (*.srt)|*.srt|sub files (*.sub)|*.sub|All files (*.*)|*.*";
            
                     
            if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFile1.FileName.Length > 0)
            {
                textBox1.Text = openFile1.FileName;
            }

            StreamReader reader = new StreamReader(openFile1.FileName); // Wycztanie pliku do textbox z CP1250
            textBox2.Text = File.ReadAllText(textBox1.Text, Encoding.GetEncoding(1250));
            reader.Close();
        }

        private void button2_Click(object sender, EventArgs e)  // Konwersja 
        {

            //string[] numbers = Regex.Split(textBox2.Text, @"\D+");
            //int anInteger;
            //anInteger = Convert.ToInt32(tbOffset.Text);
            //anInteger = int.Parse(tbOffset.Text);
            //foreach (string value in numbers)
            //{
            //    if (!string.IsNullOrEmpty(value))
            //    {

            //        int i = int.Parse(value);
            //        i += anInteger;
            //        textBox3.Text += i;
                    
            //    }
            //}

           if (comboBox1.SelectedIndex == 0)
            {
                textBox2.Text = textBox2.Text.Replace("[", "{");
                textBox2.Text = textBox2.Text.Replace("]", "}");
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                textBox2.Text = textBox2.Text.Replace("{", "[");
                textBox2.Text = textBox2.Text.Replace("}", "]");
            } 
        }
        private void button3_Click(object sender, EventArgs e) // Zapisanie pliku z cp1250
        {
            Encoding enc = Encoding.GetEncoding(1250);
            StreamWriter sw = new StreamWriter(textBox1.Text + ".tmp", false, enc);
            sw.WriteLine(textBox2.Text);
            sw.Close();
            System.IO.File.Move(textBox1.Text, textBox1.Text + ".tmp2");
            System.IO.File.Move(textBox1.Text + ".tmp", textBox1.Text);
            System.IO.File.Move(textBox1.Text + ".tmp2", textBox1.Text + ".tmp");

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (comboBox1.SelectedIndex == 0)
            {
                textBox2.Text = textBox2.Text.Replace("]", "}");
                textBox2.Text = textBox2.Text.Replace("[", "{");
            }

            else if (comboBox1.SelectedIndex == 1)
            {
                textBox2.Text = textBox2.Text.Replace("}", "]");
                textBox2.Text = textBox2.Text.Replace("{", "[");
                
            } 
        }

 

        private void tbOffset_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
             if (!Char.IsDigit(ch) && ch != 8 && ch != 45) // 8 = backspace, 45 = minus
             {
                 e.Handled = true;
             } 
        }
     }
}